package barbershop.com.controller;

import barbershop.com.entity.Vip;
import barbershop.com.service.VipService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/vip")
public class VipController {
    @Autowired
    private VipService vipService;

    // 根据Vip的手机号查找是否存在该Vip
    @RequestMapping("/findByTelNum")
    public Vip findByTelNum(@RequestParam(required = false)String telNumber){
        return this.vipService.findByTelNum(telNumber);
    }

    @RequestMapping("/findLikeVip")
    public  List<Vip> findLikeVip(@RequestParam(required = false)String vipName){
        return this.vipService.findVipByNameLike(vipName);
    }

    @RequestMapping("/allVips")
    public List<Vip> getAllVips(){
        return this.vipService.getAll();
    }

    @RequestMapping("/getPage")
    public Page<Vip> getPage(@RequestParam(value = "page") Integer page,
                             @RequestParam(value = "size") Integer size){
        return this.vipService.getPage(page, size);
    }

    @RequestMapping("/updateBalance")
    public Object updateBalance(@RequestParam(value = "vipId") Integer vipId,
                                @RequestParam(value = "balance") Double balance){
        JSONObject object = new JSONObject();
        this.vipService.updateBalance(balance, vipId);
        object.put("message", "SUCCESS");
        return object;
    }

    @RequestMapping("/add")
    public Vip addVip(@RequestBody Vip record){
        return this.vipService.insert(record);
    }

    @RequestMapping("/delete")
    public Boolean deleteVip(@RequestBody Vip record){
        return this.vipService.delete(record);
    }

    @RequestMapping("/update")
    public Vip updateVip(@RequestBody Vip record){
        return this.vipService.update(record);
    }

    @RequestMapping("/find")
    public Vip findVip(@RequestBody Vip record){
        return this.vipService.find(record);
    }
}
